package org.apache.felix.dm.impl.dependencies.bi;

import java.util.Properties;

import org.apache.felix.dm.Component;
import org.apache.felix.dm.Dependency;

/**
 * Builder returned when creating a Controllable Dependency for an Adapter
 * @since 4.0
 */
public class CallbackDependencyAdapterBuilder {

	private final Component component;

	private final AdapterCallbackHandler adapterCallback;

	public CallbackDependencyAdapterBuilder(Component component, AdapterCallbackHandler adapterCallback) {
		this.adapterCallback = adapterCallback;
		this.component = component;
	}

	/**
	 * The Controllable Dependency component that has been created
	 * @return
	 */
	public Component getComponent() {
		return component;
	}

	/**
	 * Add additional dependencies for the specified capability interface. Dependencies should be created with DependencyCallBackmanager and should
	 * NOT be instance bound.
	 * @param capabilityInterface
	 * @param dependencies
	 */
	public void addCapabilityDependencies(Class<?> capabilityInterface, Dependency... dependencies) {
		if (component.getServiceRegistration() != null) {
			throw new IllegalStateException("Cannot add instance bound dependencies on a component that has already been registered.");
		}
		adapterCallback.addCapabilityDepdendencies(capabilityInterface, dependencies);
	}

	/**
	 * Set the callback methods that are called when the Capability interface init/starts/stops/destroys
	 * @param capabilityInterface
	 * @param init
	 * @param start
	 * @param stop
	 * @param destroy
	 */
	public void setCapabilityCallbacks(Class<?> capabilityInterface, String init, String start, String stop, String destroy) {
		if (component.getServiceRegistration() != null) {
			throw new IllegalStateException("Cannot add instance bound dependencies on a component that has already been registered.");
		}
		adapterCallback.setCapabilityCallbacks(capabilityInterface, init, start, stop, destroy);
	}

	/**
	 * Set the properties that should be registered for the specified capability interface
	 * @param capabilityInterface
	 * @param properties
	 */
	public void setCapabilityServiceProperties(Class<?> capabilityInterface, Properties properties) {
		if (component.getServiceRegistration() != null) {
			throw new IllegalStateException("Cannot add instance bound dependencies on a component that has already been registered.");
		}
		adapterCallback.setCapabilityServiceProperties(capabilityInterface, properties);
	}

}
